/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.core
{
	import funit.core.events.AsyncEvent;
	import funit.core.events.TestLoadEvent;
	import funit.listeners.TestDispatcher;
	
	[Event(name="load", type="funit.core.events.TestLoadEvent")]
	[Event(name="unload", type="funit.core.events.TestLoadEvent")]
	
	public class TestRunner extends TestDispatcher implements ITestRunner
	{
		private var _id:int;
		private var _test:ITest;
		private var _listener:ITestListener;
		private var _isRunning:Boolean;
		
		
		/**
		 * @param id
		 */
		public function TestRunner( id:int = 0 )
		{
			_id = id;
		}
		
		
		/**
		 * @param test
		 */
		public function load( test:ITest ) : void
		{
			unload();
			
			if (test != null)
			{
				_test = test;
				
				dispatchEvent( new TestLoadEvent(TestLoadEvent.LOAD, test) );
			}
		}
		
		/**
		 * 
		 */
		public function unload() : void
		{
			if (_test != null)
			{
				var unloadingTest:ITest = _test;
				
				// TODO: Proper test teardown is required here.
				_test = null;
				
				dispatchEvent( new TestLoadEvent(TestLoadEvent.UNLOAD, unloadingTest) );
			}
		}
		
		/**
		 * @param listener
		 */
		public function run( listener:ITestListener = null ) : void
		{
			_listener = listener;
			
			// TODO: Potential memory leak or other unexpected behaviors if the test 
			//		 is cancelled, unloaded, or changed prior to run completion.
			//		 The listener must be removed under these conditions.
			_test.addEventListener(
				AsyncEvent.FINISH,
				function( event:AsyncEvent ) : void
				{
					_test.removeEventListener(AsyncEvent.FINISH, arguments.callee);
					
					runFinished(event.result);
				}
			);
			
			runStarted(_test.testName.name, _test.testCount);
			
			_test.run(this);
		}
		
		/**
		 * 
		 */
		public function cancel() : void
		{
			
		}
		
		/**
		 * @param testName
		 * @param testCount
		 */
		override public function runStarted( testName:String, testCount:int ) : void
		{
			_isRunning = true;
			
			if (_listener != null)
			{
				_listener.runStarted(testName, testCount);
			}
			
			super.runStarted(testName, testCount);
		}
		
		/**
		 * @param result
		 */
		override public function runFinished( result:TestResult ) : void
		{
			_isRunning = false;
			
			if (_listener != null)
			{
				_listener.runFinished(result);
			}
			
			super.runFinished(result);
		}
		
		/**
		 * @param error
		 */
		override public function runFinishedError( error:Error ) : void
		{
			_isRunning = false;
			
			if (_listener != null)
			{
				_listener.runFinishedError(error);
			}
			
			super.runFinishedError(error);
		}
		
		/**
		 * @param testName
		 */
		override public function testStarted( testName:TestName ) : void
		{
			if (_listener != null)
			{
				_listener.testStarted(testName);
			}
			
			super.testStarted(testName);
		}
		
		/**
		 * @param result
		 */
		override public function testFinished( result:TestResult ) : void
		{
			if (_listener != null)
			{
				_listener.testFinished(result);
			}
			
			super.testFinished(result);
		}
		
		/**
		 * @param testName
		 */
		override public function suiteStarted( testName:TestName ) : void
		{
			if (_listener != null)
			{
				_listener.suiteStarted(testName);
			}
			
			super.suiteStarted(testName);
		}
		
		/**
		 * @param result
		 */
		override public function suiteFinished( result:TestResult ) : void
		{
			if (_listener != null)
			{
				_listener.suiteFinished(result);
			}
			
			super.suiteFinished(result);
		}
		
		/**
		 * @param error
		 */
		override public function unhandledError( error:Error ) : void
		{
			if (_listener != null)
			{
				_listener.unhandledError(error);
			}
			
			super.unhandledError(error);
		}
		
		/**
		 * @param testOutput
		 */
		override public function testOutput( testOutput:TestOutput ) : void
		{
			if (_listener != null)
			{
				_listener.testOutput(testOutput);
			}
			
			super.testOutput(testOutput);
		}
		
		
		/**
		 * 
		 */
		public function get id() : int
		{
			return _id;
		}
		
		
		/**
		 * 
		 */
		public function get test() : ITest
		{
			return _test;
		}
		
		
		/**
		 * 
		 */
		public function get isRunning() : Boolean
		{
			return _isRunning;
		}
		
		
		/**
		 * 
		 */
		public function get testCount() : int
		{
			return (_test != null) ? _test.testCount : 0;
		}
		
	}
	
}
